import * as types from "../action-types";
import { reqUserInfo } from "@/api/user";

export const getUserInfo = () => (dispatch) => {
  return new Promise((resolve, reject) => {
    reqUserInfo()
      .then((response) => {
        const { data } = response;
        const userInfo = data.user;
        dispatch(setUserInfo(userInfo));
        resolve(data);
      })
      .catch((error) => {
        reject(error);
      });
  });
};

export const setUserToken = (token) => {
  return {
    type: types.USER_SET_USER_TOKEN,
    token,
  };
};

export const setUserPermissions = (permissions) => {
  // 本地存一份 只有登录接口才返回 防止刷新
  localStorage.setItem('permissions', JSON.stringify({ permissions}));
  return {
    type: types.USER_SET_USER_PERMISSIONS,
    permissions,
  };
};

export const setUserInfo = (userInfo) => {
  return {
    type: types.USER_SET_USER_INFO,
    ...userInfo,
  };
};

export const resetUser = () => {
  return {
    type: types.USER_RESET_USER,
  };
};


